import Foundation
public extension String {
    var color: UIColor {
        return (self as NSString).makeColor()
    }
    var image: UIImage? {
        return (self as NSString).makeImage()
    }
    var thousanded: String {
        return (self as NSString).currencyGrouped()
    }
    var minusStyle: String {
        return self == "0" ? self : "-\(self.replacingOccurrences(of: "-", with: "").thousanded)"
    }
    var empty: Bool {
        return self.isEmpty || self == "0"
    }
    var fixEmpty: String {
        return self.isEmpty ? "-" : self
    }
}
public extension UITextField {
    func checkPhoneRules() {
        self.fixPureNumber()
        let text = self.text ?? ""
        if text.count > 0 {
            guard text.hasPrefix("6") || text.hasPrefix("7") || text.hasPrefix("8") || text.hasPrefix("9") else {
                SVProgressHUD.showError(withStatus: "Please enter correct phone number")
                self.text = ""
                return
            }
        }
        if text.count > 10 {
            let toIndex = text.index(text.startIndex, offsetBy: 10)
            self.text = String(text[text.startIndex..<toIndex])
        }
    }
    func fixPureNumber() {
        let text = self.text ?? ""
        let characterSet = NSCharacterSet.decimalDigits.inverted
        let array = text.components(separatedBy: characterSet)
        self.text = array.joined()
    }
    func checkCodeRules() {
        self.fixPureNumber()
        let text = self.text ?? ""
        if text.count > 6 {
            let toIndex = text.index(text.startIndex, offsetBy: 6)
            self.text = String(text[text.startIndex..<toIndex])
        }
    }
    func checkPhone() -> Bool {
        let text = self.text ?? ""
        if text.count == 0 {
            SVProgressHUD.showError(withStatus: "Please enter mobile number")
            return false
        } else {
            if text.hasPrefix("6") || text.hasPrefix("7") || text.hasPrefix("8") || text.hasPrefix("9") {
                if text.count != 10 {
                    SVProgressHUD.showError(withStatus:  "Please enter correct phone number")
                    return false
                }
            }
        }
        return true
    }
    func checkCode() -> Bool {
        let text = self.text ?? ""
        if text.count == 0 {
            SVProgressHUD.showError(withStatus:  "Enter OTP")
            return false
        } else {
                if text.count != 6 {
                    SVProgressHUD.showError(withStatus:  "Please enter correct OTP")
                    return false
                }
        }
        return true
    }
}
public enum FYOrderDetailRows: Int, CaseIterable {
    case loanAmount
    case dueDate
    case loanTerm
    case serviceFee
    case interest
    case amountRecevied
    case dateOfApply
    case paymentDate
    case lateCharge
    case totalRepayment
    case amountOfDeduction
    case amountRepaid
    case defermentCharge
    case amountDue
    case repaymentDate
    var title: String {
        switch self {
        case .loanAmount:
            return "Loan amount"
        case .dueDate:
            return "Due date"
        case .loanTerm:
            return "Loan term"
        case .serviceFee:
            return "Service fee"
        case .interest:
            return "Interest"
        case .amountRecevied:
            return "Amount recevied"
        case .dateOfApply:
            return "Date of application"
        case .paymentDate:
            return "Payment date"
        case .lateCharge:
            return "Late charge"
        case .totalRepayment:
            return "Total repayment"
        case .amountOfDeduction:
            return "Amount of deduction"
        case .amountRepaid:
            return "Amount repaid"
        case .amountDue:
            return "Amount due"
        case .repaymentDate:
            return "Repayment date"
        case .defermentCharge:
            return "Deferment charge"
        }
    }
    func content(by order: FYOrderDetailOrder) -> String {
        switch self {
        case .loanAmount:
            return order.loanAmount.thousanded
        case .dueDate:
            return order.dueDate.fixEmpty
        case .loanTerm:
            return "\(order.showTerm) Days"
        case .serviceFee:
            return order.feeAmount.thousanded
        case .interest:
            return order.taxAmount.thousanded
        case .amountRecevied:
            return order.receiptAmount.thousanded
        case .dateOfApply:
            return order.applyDate.fixEmpty
        case .paymentDate:
            return order.payoutDate.fixEmpty
        case .lateCharge:
            return order.penaltyAmount.thousanded
        case .totalRepayment:
            return order.totalRepaymentAmount.thousanded
        case .amountOfDeduction:
            return order.reductionAmount.minusStyle
        case .amountRepaid:
            return order.alreadyRepaymentAmount.thousanded
        case .amountDue:
            return order.shouldRepaymentAmount.thousanded
        case .repaymentDate:
            return order.repaymentDate.fixEmpty
        case .defermentCharge:
            return order.repayExtensionFeeAmount.thousanded
        }
    }
}
